//
//  PKPageContentView.m
//  Fanke
//
//  Created by sharayuki on 2017/8/14.
//  Copyright © 2017年 sharayuki. All rights reserved.
//

#import "PKPageContentView.h"

@implementation PKPageContentView{
//上部导航条
    UIScrollView *_pageBar;
//内容视图
    UIScrollView *_contentView;
//pageBaritemButton集合
    NSMutableArray <UIButton *> *_pageBarItemButtons;
// 内容视图数据容器
    NSMutableArray<UIView *>    *_contentItemViews;
}
- (CGFloat)pageBarHeight{

    CGFloat height = 30;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageBarHeightInPageContentView:)]) {
        
        height = [self.delegate pageBarHeightInPageContentView:self];
    }
    
    return height;
    
}
- (NSInteger)numberOfItems{

    NSInteger count = 0;
    
    if (self.dataSource &&[self.dataSource respondsToSelector:@selector(numberOfItemsInPageContentView:)]) {
        
        count = [self.dataSource numberOfItemsInPageContentView:self];
    }
    
    return count;
}
// 返回上层提供内容
- (UIView *)contentItemViewAtIndex:(NSInteger)index {
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(pageContentView:contentViewAtIndex:)]) {
        return [self.dataSource pageContentView:self contentViewAtIndex:index];
    }
    return [UIView new];
    
}

/**
   刷新数据和界面的方法
*/
- (void)reloadData{
/**
 初始化PageBar
 */
    //初始化变量
    if (_pageBarItemButtons == nil){
        _pageBarItemButtons = [NSMutableArray array];
    }else{
        for (UIButton *itemButton in _pageBarItemButtons) {
            
            [itemButton removeFromSuperview];
            
        }
        [_pageBarItemButtons removeAllObjects];
    }
    if (_pageBar == nil) {
        
        _pageBar = [UIScrollView new];
    
       // _pageBar.backgroundColor = [UIColor blueColor];
        [self addSubview:_pageBar];
        
        [_pageBar mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.right.equalTo(self);
            
            make.height.mas_equalTo([self pageBarHeight]);
            
        }];
    }else{
    
        [_pageBar mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.right.equalTo(self);
            
            make.height.mas_equalTo([self pageBarHeight]);
            
        }];
    }
    [_pageBar removeAllSubviews];
    //遍历初始化界面
    NSInteger count = [self numberOfItems];
    
    for (int i = 0; i < count; i++) {
        
        UIButton *pageBarItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        pageBarItemButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        if (self.dataSource && [self.dataSource respondsToSelector:@selector(pageContentView:titleAtIndex:)]) {
            
            [pageBarItemButton setTitle:[self.dataSource pageContentView:self titleAtIndex:i] forState:UIControlStateNormal];

        }
        [_pageBarItemButtons addObject:pageBarItemButton];
        
        [_pageBar addSubview:pageBarItemButton];
    }
    CGFloat itemPrecent = 1.0 / count;
    //遍历itemButton数组进行pageBar布局
    for (NSInteger i = 0; i < _pageBarItemButtons.count; i++) {
        
        UIButton *itemButton = _pageBarItemButtons[i];
        
        
        [itemButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [itemButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (i == 0) {
                
                make.left.equalTo(_pageBar);
                
            }else{
            
                make.left.equalTo(_pageBarItemButtons[i - 1].mas_right);
            }
            if (i == _pageBarItemButtons.count - 1) {
                
                make.right.equalTo(_pageBar);
            }else{
            
                make.right.equalTo(_pageBarItemButtons[i + 1].mas_left);
            }
            
            make.top.bottom.equalTo(_pageBar);
            
            make.height.equalTo(_pageBar);
            
            make.width.equalTo(_pageBar).multipliedBy(itemPrecent);
            
        }];
    }
    /**
     初始化内容视图
     */
    //初始化内容容器
    if (_contentItemViews == nil) {
        
        _contentItemViews = [NSMutableArray array];
    }else{
        [_contentItemViews removeAllObjects];
    }
    //初始化内容视图
    if (_contentView == nil) {
        
        _contentView = [UIScrollView new];
        _contentView.pagingEnabled = YES;
        _contentView.showsVerticalScrollIndicator = NO;
        _contentView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_contentView];
        
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            
            make.top.equalTo(_pageBar.mas_bottom);
        }];
    }
    
    [_contentView removeAllSubviews];
    
    for (NSInteger i = 0; i < count; i++) {
        
        UIView *contentItemView = [self contentItemViewAtIndex:i];
        
        [_contentView addSubview:contentItemView];
        [_contentItemViews addObject:contentItemView];
    }
    // 对内容进行布局
    for (NSInteger i = 0; i < count; i ++) {
        UIView *contentItemView = _contentItemViews[i];
        [contentItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if(i == 0) {
                make.left.equalTo(_contentView);
            } else {
                make.left.equalTo(_contentItemViews[i - 1].mas_right);
            }
            
            if(i == count - 1) {
                make.right.equalTo(_contentView);
            } else {
                make.right.equalTo(_contentItemViews[i + 1].mas_left);
            }
            make.top.bottom.height.width.equalTo(_contentView);
            
        }];
    }
}
@end
